package plugin

import (
	"context"
	"github.com/cloudwego/hertz/pkg/common/hlog"
	jsoniter "github.com/json-iterator/go"
)

// 日志插件（前后置插件）
type Log struct {
}

// PreCall handles rpc call from clients
func (this *Log) PreCall(ctx context.Context, serviceName, methodName string, args interface{}) (interface{}, error) {
	reqStr, _ := jsoniter.MarshalToString(args)
	hlog.Debugf("rpc[%s:%s] req:%s", serviceName, methodName, reqStr)
	return args, nil
}

func (this *Log) PostCall(ctx context.Context, serviceName, methodName string, args, reply interface{}, err error) (interface{}, error) {
	if err != nil {
		hlog.Errorf("rpc[%s:%s] err:%s", serviceName, methodName, err.Error())
	} else {
		respStr, _ := jsoniter.MarshalToString(reply)
		hlog.Debugf("rpc[%s:%s] resp:%s", serviceName, methodName, respStr)
	}
	return reply, nil
}
